
//
//  GiftTopUpView.m
//  CheekiOS
//
//  Created by mac on 2017/7/22.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "GiftTopUpView.h"

@interface GiftTopUpView ()


@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UIButton *charmButton;
@property (nonatomic, strong) UIButton *moneyButton;
@property (nonatomic, strong) UIButton *backGiftButton;

@end

@implementation GiftTopUpView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.bgView = [[UIView alloc] init];
        self.bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_bgView];
        
        self.nameLab = [[UILabel alloc] init];
        self.nameLab.backgroundColor = Colorbackground;
        self.nameLab.font = FontP(17);
        self.nameLab.textAlignment = NSTextAlignmentCenter;
        self.nameLab.text = @"充值";
        self.nameLab.textColor = [UIColor blackColor];
        [_bgView addSubview:self.nameLab];
        
        self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.backButton];
        
        self.backGiftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.backGiftButton addTarget:self action:@selector(backGiftButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.backGiftButton setBackgroundImage:[UIImage imageNamed:@"viedo_recharge_backgift_button"] forState:UIControlStateNormal];
        [self.backGiftButton setTitle:@"返回礼物" forState:0];
        self.backGiftButton.titleLabel.font = FontP(15);
        [self.backGiftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_bgView addSubview:self.backGiftButton];

    
    }
    return self;
}




-(void)setCharmArray:(NSArray *)charmArray{
    self.bgView.frame = CGRectMake(0, KScreenHeight-230*SCREEN_WIDTH_RATIO47, KScreenWidth, 230*SCREEN_WIDTH_RATIO47);
    self.nameLab.frame = CGRectMake(0,0, KScreenWidth, 50*SCREEN_WIDTH_RATIO47);
    self.backButton.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight-self.bgView.height);
    
    CGFloat charmBtnK = 20*SCREEN_WIDTH_RATIO47;
    CGFloat charmBtnW = (KScreenWidth-115*SCREEN_WIDTH_RATIO47)/4;
    CGFloat charmBtnY = _nameLab.bottom+19;
    CGFloat charmBtnH = 30*SCREEN_WIDTH_RATIO47;
    
    for (int i = 0; i<charmArray.count; i++) {
        self.charmButton = [UIButton buttonWithType:UIButtonTypeCustom];
     
        self.charmButton.titleLabel.font = FontP(16);
        [self.charmButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.charmButton setImage:[UIImage imageNamed:@"grzx_account_diamond_icon"] forState:UIControlStateNormal];
        self.charmButton.tag = i;
        [self.charmButton setTitle:charmArray[i] forState:UIControlStateNormal];
        self.charmButton.frame = CGRectMake(charmBtnK+(i*(charmBtnW+charmBtnK+5*SCREEN_WIDTH_RATIO47)), charmBtnY, charmBtnW, charmBtnH);
        [self.charmButton addTarget:self action:@selector(moneyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:self.charmButton];
    }
    
    
}

-(void)setMoneyArray:(NSArray *)moneyArray{
    CGFloat charmBtnK = 20*SCREEN_WIDTH_RATIO47;
    CGFloat charmBtnW = (KScreenWidth-115*SCREEN_WIDTH_RATIO47)/4;
    CGFloat charmBtnY = _charmButton.bottom+10;
    CGFloat charmBtnH = 30*SCREEN_WIDTH_RATIO47;
    
    for (int i = 0; i<moneyArray.count; i++) {
        self.moneyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        self.moneyButton.titleLabel.font = FontP(16);
        [self.moneyButton setTitleColor:Colorbackground forState:UIControlStateNormal];
        self.moneyButton.tag = i;
        [self.moneyButton setTitle:[NSString stringWithFormat:@"￥%@",moneyArray[i]] forState:UIControlStateNormal];
        self.moneyButton.frame = CGRectMake(charmBtnK+(i*(charmBtnW+charmBtnK+5*SCREEN_WIDTH_RATIO47)), charmBtnY, charmBtnW, charmBtnH);
        self.moneyButton.layer.cornerRadius = 15;
        self.moneyButton.layer.masksToBounds = YES;
        self.moneyButton.layer.borderColor = Colorbackground.CGColor;
        self.moneyButton.layer.borderWidth = 1;
        [self.moneyButton addTarget:self action:@selector(moneyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:self.moneyButton];
    }
    self.backGiftButton.frame = CGRectMake(20*SCREEN_WIDTH_RATIO47, self.moneyButton.bottom+38*SCREEN_WIDTH_RATIO47, 70*SCREEN_WIDTH_RATIO47, 35*SCREEN_WIDTH_RATIO47);

}

- (void)backButtonClick{
    CATransition *animation = [CATransition  animation];
    
    animation.duration = 0.2f;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromBottom;
    [self.layer addAnimation:animation forKey:@"animtion2"];
    [UIView animateWithDuration:0.2 animations:^{
        
    }completion:^(BOOL finished) {
        [self removeFromSuperview];

    }];

}

- (void)backGiftButtonClick{
    if ([self.delegate respondsToSelector:@selector(backGiftButtonClick)]) {
        [self.delegate backGiftButtonClick];
    }
    

}
-(void)moneyButtonClick:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(moneyButtonClick:)]) {
        [self.delegate moneyButtonClick:sender];
    }
    
}
@end
